import axios from 'axios'
axios = axios.create({
	baseURL:'/api',
	timeout:5000
})
export default function ajax(url,data={},type="GET"){
	// console.log('url:'+ url,'data:' + data, 'type:' + type)
	  return new Promise(function(resolve,reject){
		let promise 
		if (type === 'GET') {
			// get 请求
			let dataStr = '' 
			Object.keys(data).forEach(key => {
				dataStr += key + '=' + data[key] + '&'
			})
			if (dataStr !== '') {
				dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
				url = url + '?' + dataStr
			}
			 promise = axios.get(url)
		} else {
			// post 请求
			 promise = axios.post(url,data)
		}
		
		promise.then(function(response){
			// console.log(response)
			resolve(response.data)
		}).catch(function(error){
			console.log(error)
			reject(error)
		})
	})
}